package com.kichina.parking.module.message.http;

import com.alibaba.fastjson.JSONObject;
import com.kichina.parking.controller.base.BaseController;
import com.kichina.parking.controller.base.BaseUpstreamResponse;
import com.kichina.parking.module.message.socket.enums.MessageTypeEnum;
import com.kichina.parking.module.message.socket.IMessageHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Surging
 * @version 1.0
 * @ClassName ParkingController
 * @date 2022/6/14 15:44
 * @desc 停车管理上行接口控制器
 */
@RestController()
public class ParkingController extends BaseController {


    private final List<IMessageHandler> socketMessageHandlers;

    public ParkingController(List<IMessageHandler> socketMessageHandlers) {
        this.socketMessageHandlers = socketMessageHandlers;
    }


    @PostMapping("/parking")
    public BaseUpstreamResponse uploadAdmissionRecords(@RequestBody JSONObject jsonObject) {
        System.out.println("http 接收数据上行控制处理链" + jsonObject.toString());

        if (!jsonObject.containsKey("service")) {
            return BaseUpstreamResponse.buildFailed();
        }
        for (IMessageHandler iMessageHandler : socketMessageHandlers) {
            if (iMessageHandler.check(jsonObject)){
                try {
                    iMessageHandler.handler(jsonObject,null);
                }catch (Exception e) {
                    return buildResponseFailed(jsonObject);
                }
               
                if (iMessageHandler.interrupt()){
                    return buildResponseSuccess(jsonObject);
                }
            }
        }
        return BaseUpstreamResponse.buildFailed();
    }

    private BaseUpstreamResponse buildResponseSuccess(JSONObject jsonObject) {
        if (MessageTypeEnum.UPLOAD_BLAKE_LIST.getValue().equals(jsonObject.getString("service"))) {
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        } else if (MessageTypeEnum.UPLOAD_CARD_TYPE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        } else if (MessageTypeEnum.UPLOAD_CAR_TYPE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        } else if ( MessageTypeEnum.UPLOAD_GATE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        } else if (MessageTypeEnum.UPLOAD_MOBILE_CHARGE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"), jsonObject.getString("order_id"), jsonObject.getInteger("mobilechargeid"));
        } else if (MessageTypeEnum.MONTH_PAY_RECORD.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccessOrderId(jsonObject.getString("service"), jsonObject.getString("orderid"));
        } else if (MessageTypeEnum.MONTH_PAY_RULE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccessRuleId(jsonObject.getString("service"), jsonObject.getString("orderid"));
        } else if (MessageTypeEnum.UPLOAD_OPEN_STROBE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"), jsonObject.getString("recordid"));
        } else if (MessageTypeEnum.UPLOAD_OPERATOR.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccessRecordId(jsonObject.getString("service"), jsonObject.getString("recordid"));
        } else if (MessageTypeEnum.UPLOAD_CAR_IN.getValue().equals(jsonObject.getString("service"))
                ||MessageTypeEnum.UPLOAD_CAR_OUT.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"), jsonObject.getString("order_id"));
        } else if (MessageTypeEnum.PARKING_SPACE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        }else if (MessageTypeEnum.REQUEST_AUTO_PAY.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        }else if (MessageTypeEnum.REQUEST_BARCODE_PAY.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        }else if (MessageTypeEnum.UPLOAD_TICKET.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        }else if (MessageTypeEnum.UPLOAD_WHITE_LIST.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        }else {
            return BaseUpstreamResponse.buildSuccess(jsonObject.getString("service"));
        }
    }

    private BaseUpstreamResponse buildResponseFailed(JSONObject jsonObject) {
        if (MessageTypeEnum.UPLOAD_BLAKE_LIST.getValue().equals(jsonObject.getString("service"))) {
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        }else if (MessageTypeEnum.UPLOAD_CARD_TYPE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        } else if (MessageTypeEnum.UPLOAD_CAR_TYPE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        } else if ( MessageTypeEnum.UPLOAD_GATE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        } else if (MessageTypeEnum.UPLOAD_MOBILE_CHARGE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"), jsonObject.getString("order_id"), jsonObject.getInteger("mobilechargeid"));
        } else if (MessageTypeEnum.MONTH_PAY_RECORD.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailedOrderId(jsonObject.getString("service"), jsonObject.getString("orderid"));
        } else if (MessageTypeEnum.MONTH_PAY_RULE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailedRuleId(jsonObject.getString("service"), jsonObject.getString("orderid"));
        } else if (MessageTypeEnum.UPLOAD_OPEN_STROBE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"), jsonObject.getString("recordid"));
        } else if (MessageTypeEnum.UPLOAD_OPERATOR.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailedRecordId(jsonObject.getString("service"), jsonObject.getString("recordid"));
        } else if (MessageTypeEnum.UPLOAD_CAR_IN.getValue().equals(jsonObject.getString("service"))
                ||MessageTypeEnum.UPLOAD_CAR_OUT.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"), jsonObject.getString("order_id"));
        } else if (MessageTypeEnum.PARKING_SPACE.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        }else if (MessageTypeEnum.REQUEST_AUTO_PAY.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        }else if (MessageTypeEnum.REQUEST_BARCODE_PAY.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        }else if (MessageTypeEnum.UPLOAD_TICKET.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        }else if (MessageTypeEnum.UPLOAD_WHITE_LIST.getValue().equals(jsonObject.getString("service"))){
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        }else {
            return BaseUpstreamResponse.buildFailed(jsonObject.getString("service"));
        }

    }


}
